﻿using AppManager.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using AppManager.IDAL;
using System.Web;

namespace AppManager.DAL
{
    public class AppRepository : BaseRepository<App>, IAppRepository
    {
        /// <summary>
        /// 數據的createdate和updatedate這兩個參數都放到dal中來處理
        /// </summary>
        /// <param name="app"></param>
        /// <param name="infos"></param>
        /// <returns></returns>
        public int AddOrUpdate(App app, List<AppInfo> infos)
        {
            //這種也是錯誤的，因為他至少不像我想要的结果那样，因为他会导致复合数据，
            //如果有一天他告诉你这个东西的导航属性就是他自己查询了一遍然后赋值的
            var list1 = from a in dbSet
                        join b in db.Set<AppInfo>() on a.Id equals b.AppId
                        select a;

            //这种写法是错的，因为AppInfos不是导航属性,因為我不想他是一個外鍵的強關係
            var list2 = from a in dbSet.Include(s => s.AppInfos) select a;

            var m1 = list1.FirstOrDefault();
            //var m2 = list2.FirstOrDefault();

            //使用該屬性表示嵌套事務中不創建新的事務，也就是在所有嵌套的方法中所有的操作在一個事務中
            using (var tran = GetTransaction())
            {
                if (app.Id > 0)
                {
                    app.UpdateDate = DateTime.Now;
                    db.Entry(app).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    app.CreateDate = DateTime.Now;
                    db.Entry(app).State = System.Data.Entity.EntityState.Added;
                }

                db.SaveChanges();
                if (infos.All(s => s.Id > 0))
                {
                    foreach (var item in infos)
                    {
                        item.UpdateDate = DateTime.Now;
                        db.Entry(item).State = EntityState.Modified;
                    }
                }
                else
                {
                    foreach (var item in infos)
                    {
                        item.CreateDate = DateTime.Now;
                        item.AppId = app.Id;
                        db.Entry(item).State = EntityState.Added;
                    }
                }
                db.SaveChanges();
                tran.Complete();
                return app.Id;
            }
        }

        public void DeleteModel(int userId, App app)
        {
            List<string> files = new List<string>();
            using (var tran = GetTransaction())
            {
                files.Add(app.AppLogo);
                db.Entry(app).State = EntityState.Deleted;
                var infos = AppInfoDAL.Current.GetList(s => s.AppId == app.Id && s.CreateUserId == userId);
                foreach (var item in infos)
                {
                    files.Add(item.TestUrl);
                    db.Entry(item).State = EntityState.Deleted;
                }

                foreach (var path in files)
                {
                    var serverPath = HttpContext.Current.Server.MapPath(path);
                    if (File.Exists(serverPath))
                        File.Delete(serverPath);
                }

                db.SaveChanges();
                tran.Complete();
            }
        }
    }

    public class AppInfoDAL : BaseRepository<AppInfo>
    {
    }
}
